package com.yongqi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yongqi.model.Family;
import com.yongqi.model.back.Email;
import com.yongqi.pub.PrincipalFormat;
import com.yongqi.service.back.EmailService;

@Controller
@RequestMapping("/email")
public class EmailController {
	@Autowired
	EmailService emailService;
	@RequestMapping("/index")
	public String emailIndex() {
		return "email/EmailIndex";
	}
	@ResponseBody
	@RequestMapping("/query")
	public String emailQuery(HttpServletRequest request,@RequestParam int pageNumber ,int pageSize,String email_to,String email_subject,String date,String dateEnd) {
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		map.put("familyAccount", familyAccount);
//		map.put("readed", "0");
		if (email_to != null || email_to!= "") {
			map.put("email_to", email_to);
		}
		if (email_subject != null || email_subject!= "") {
			map.put("email_subject", email_subject);
		}
		if (date != null || date!= "") {
			map.put("date", date);
		}
		if (dateEnd != null || dateEnd!= "") {
			map.put("dateEnd", dateEnd);
		}
		int total = emailService.emailPushQuery(map).size();
		PageHelper.startPage(pageNumber,pageSize);
		List<Email> emails = emailService.emailQuery(map);
		JSONObject object = new JSONObject();
		object.put("rows", emails);
		object.put("total", total);
		return object.toJSONString();
	}
	/**
	 * 修改邮件状态
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/changeReaded")
	public String changeReaded(HttpServletRequest request) {
		System.out.println("changeReaded");
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		String id = request.getParameter("id");
		map.put("familyAccount", familyAccount);
		map.put("id", id);
		emailService.changeReaded(map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
	/**
	 * 删除邮件
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/deleteEmail")
	public String deleteEmail(HttpServletRequest request) {
		System.out.println("deleteEmail");
		Map<String, String> map = new HashMap<String, String>();
		String familyAccount = ((Family) SecurityUtils.getSubject().getPrincipal()).getFamilyaccount();
		String id = request.getParameter("id");
		map.put("familyAccount", familyAccount);
		map.put("id", id);
		emailService.deleteEmail(map);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("result", "success");
		return jsonObject.toJSONString();
	}
}
